import operator
import time
from osv import osv, fields
from osv.orm import intersect
import tools.sql
from tools import config
from tools.translate import _
from mx import DateTime
import mx.DateTime
from mx.DateTime import RelativeDateTime, now, DateTime, localtime

class kdvn_history_price(osv.osv):
    _name = "kdvn.history.price"
    _description = "KDVN History Price"
    _auto=False
    _order = "product_id,partner_id"
    _columns = {
        'product_id': fields.many2one('product.product','Product'),
        'order_id':fields.many2one('purchase.order','PO'),
        'partner_id': fields.many2one('res.partner','Partner'),
        'date_order': fields.date('Date'),
        'price': fields.float('Price'),
        'product_name':fields.char('Product name',size=250),
        'project_id':fields.many2one('kdvn.project','Project'),
        'description':fields.char('Description',size=255)
        }
    
    def init(self, cr):
        cr.execute("""CREATE OR REPLACE VIEW kdvn_history_price AS
                        Select 
                            pol.id as id,product_id,
                            partner_id,
                            fncompute(currency_id,get_default_currency(),date_order,'tax_office')*price_subtotal as price,
                            date_order::date,
                            pt.name as product_name,
                            po.id as order_id,
                            pol.project_id,
                            pol.name as description
                        from 
                            purchase_order_line pol
                        left join purchase_order po on pol.order_id=po.id
                        left join product_template pt on product_id=pt.id 
                    where 
                        po.state not in ('draft','cancel');""") # ('cancel','draft')
kdvn_history_price()




class product_product(osv.osv):
    _name = "product.product"
    _inherit = "product.product"
    _columns = {
                'kdvn_price_history': fields.one2many("kdvn.history.price",'product_id',"History"),        
                    }
product_product()

class res_partner(osv.osv):
    _name = "res.partner"
    _inherit = "res.partner"
    
    _columns = {
                'kdvn_price_history_ids': fields.one2many("kdvn.history.price",'partner_id',"Price History"),        
                    }
res_partner()



class kdvn_history_data_material_purchasing(osv.osv):
    _name = "kdvn.history.data.material.purchasing"
    _description = "KDVN History Price"
    _auto=False
    
    _columns = {
                'project_name':fields.char('Project Name',size=255),
                'code':fields.char('Code',size=25),
                'product_name':fields.char('Product Name',size=255),
                'description':fields.char('Description',size=255),
                'model_no':fields.char('Model No',size=100),
                'supplier_name':fields.char('Supplier Name',size=255),
                'brandname':fields.char('Brand Name',size=100),
                'country_of_origin':fields.char('Country Of Origin',size=50),
                'currency':fields.char('Currency',size=3),
                'initial_quotation':fields.float('Initial Quotation'),
                'after_negotiation':fields.float('After Negotiation'),
                'discount_percent':fields.float('Discount Percent'),
                'qty':fields.float('Qty'),
                'unit':fields.char('Unit',size=10),
                'total_amount':fields.float('Total Amount'),
                'price_term':fields.char('Price Term',size=10),
                'po_no':fields.char('Po No',size=20),
                'po_date':fields.date('Po Date'),
                'exchange_rate_usd_to_other':fields.float('Exchange Rate USD To Other'),
                'time_of_delivery':fields.date('Time Of Delivery'),
                'created_date':fields.date('Created Date'),
                'remark':fields.char('Remark',size=255),
                'project_id':fields.many2one('kdvn.project','Project',select=1),
                'product_id':fields.many2one('product.product','Product',select=1),
                'supplier_id':fields.many2one('res.partner','Supplier'),
                'category_id':fields.many2one('kdvn.product.category.japan','Kinden Category'),
                'po_id':fields.many2one('purchase.order','P.O. ID'),
                'budget_id':fields.many2one('account.budget.post','Budget Code',select=1)
        }
kdvn_history_data_material_purchasing()